package com.xpgk.mvc.bpropaganda;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.log.Log;
import com.xpgk.annotation.Service;
import com.xpgk.constant.ConstantRender;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.bentryRegister.Customer;
import com.xpgk.mvc.bentryRegister.CustomerAgreement;
import com.xpgk.mvc.bentryRegister.CustomerProduct;
import com.xpgk.mvc.brefund.ProductReturn;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.intoPersonnel.IntoExperience;
import com.xpgk.mvc.intoPersonnel.IntoRelationship;
import com.xpgk.mvc.productOutin.ProductOutin;
import com.xpgk.mvc.productStock.ProductStock;
import com.xpgk.mvc.productStock.ProductStockInfo;
import com.xpgk.mvc.propgandaThings.PropgandaThings;
import com.xpgk.tools.ToolRandoms;


@Service(name = BpropagandaTrainingService.serviceName)
public class BpropagandaTrainingService  extends  BaseService{
	
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(BpropagandaTrainingService.class);
	public static final String serviceName = "bpropagandaTrainingService";	
	
	/**
	 * 保存宣传信息
	 * */
	public RenderBean  savePro(Bpropaganda bpropaganda,String orgids,String userIds)
	{	
	
		RenderBean result = new RenderBean();
		String  msg="";
			bpropaganda.set(Bpropaganda.column_organIds, orgids)
			.set(Bpropaganda.column_createDate, new Date())
			.set(Bpropaganda.column_createId, userIds);
			if(bpropaganda.save()){
				
				System.out.println("添加成功");
				msg=ConstantRender.render_success_code;
			
			}else{
				System.out.println("shibai ");
				msg="添加失败！";
			}
			result.getMap().put("msg", msg);
			return result;
		}
	
	
	/**
	 * 保存出库信息
	 * */

	
	public String  saveOutGoods(List<PropagandaThingsOut> ieList,String orgids){
		/**
		 * 保存出库记录
		 */
		String msg="";
		
		if(ieList!=null && ieList.size()>0)
		{	
			for(PropagandaThingsOut ie:ieList)
			{
			
				if(!ie.save())
				{
					msg=ConstantRender.render_error;
					
					throw new RuntimeException("保存出库信息失败!");
				}else{
					msg=ConstantRender.render_success_code;
					
				}
				
				/**
				 * 更新宣传品库存
				 */

				
				//String  sptSql=getSqlMy(PropgandaThings.sqlId_selPSByOidsAndPids);
			//	PropgandaThings  ps=PropgandaThings.dao.findFirst(sptSql,orgids,ie.getStr(PropagandaThingsOut.column_thingids));
				PropgandaThings  ps=PropgandaThings.dao.findById(ie.getStr(PropagandaThingsOut.column_thingids));
				if(ps!=null)
				{
					//更新
					float  num=ps.getInt(PropgandaThings.column_num)-ie.getInt(PropagandaThingsOut.column_outStorage);
					float  pricesum=(float) (ps.getDouble(PropgandaThings.column_price)*num);
					ps.set(PropgandaThings.column_num, num)
					.set(PropgandaThings.column_pricesum, pricesum);
					if (num>=0) {
						if(!ps.update())
						{
							msg="服务器异常";
							log.debug("更新产品总库存失败");
							throw new RuntimeException("更新产品总库存失败!");
						}else{
							msg=ConstantRender.render_success_code;;
						}
					}else{
						msg="库存不足，不能出库";
					}
					
					
				}else
				{
					msg="库存为0，不能出库";
					log.debug("库存为0，不能出库");
					throw new RuntimeException("库存为0，不能出库!");
				}
			}
			
			
	}else{
		msg=ConstantRender.render_error;
	}
		
	return  msg;
	
	

	}

}
	


